package com.tm.chargecar.service.impl;

import com.tm.chargecar.dto.AreaDto;
import com.tm.chargecar.entity.AreaEntity;
import com.tm.chargecar.mapper.AreaMapper;
import com.tm.chargecar.mapper.SiteDogcartMapper;
import com.tm.chargecar.result.Result;
import com.tm.chargecar.service.AreaService;
import com.tm.chargecar.service.SiteDogcartService;
import com.tm.chargecar.utils.PageUtil;
import com.tm.chargecar.vo.AreaVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wangSang
 * @create 2022-08-24 14:51
 *
 * 分区
 */
@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private AreaMapper mapper;
    @Autowired
    private SiteDogcartMapper siteDogcartMapper;



    /**
     * 片区 查询  带分页
     * @param areaVo
     * @return
     */
    @Override
    public Result quertyArea(AreaVo areaVo) {



        // 创建分页工具
        PageUtil pageUtil = new PageUtil();
        // 查询总条数
        Long  totalCouunt   =    mapper.quertyCount(areaVo);
        // 将总条数放入 工具类
        pageUtil.setTotalCount(totalCouunt);
        //  查出分页数据
        List<AreaDto> AreaEntityEntityList = mapper.quertyArea(areaVo);
        //  放入数据
        pageUtil.setData(AreaEntityEntityList);
        //循环查询
        for (int i = 0; i < AreaEntityEntityList.size(); i++) {

            Integer  count =  siteDogcartMapper.queryAreaId(AreaEntityEntityList.get(i).getAreaId());
            AreaEntityEntityList.get(i).setAreaSiteCount(count);

        }



        return Result.SUCCESS(pageUtil);
    }

    /**
     * 新增片区
     * @param areaEntity
     * @return
     */
    @Override
    public Result addArea(AreaEntity areaEntity) {

        mapper.addArea(areaEntity);


        return Result.SUCCESS("新增成功");
    }

    @Override
    public Result updateArea(AreaEntity areaEntity) {
        mapper.updateArea(areaEntity);

        return Result.SUCCESS("修改成功");
    }

    /***
     *  删除片区
     * @param id
     * @return
     */
    @Override
    public Result deleteArea(Integer id) {

        mapper.deleteArea(id);

        return Result.SUCCESS("删除成功");
    }
}
